#include "man.hpp"

#include <iostream>

Variable bi_about(vector<Variable>& argv, Environment& env)
{
	UNUSED env;
	string name = "Oratr";
	vector<Variable> dev; {
		dev.push_back((Variable)"demosthenes2k8");
		dev.push_back((Variable)"bot190");
		dev.push_back((Variable)"dysxqer");
		dev.push_back((Variable)"cgcat93");
		dev.push_back((Variable)"asterios");
	}
	string version = "20101108";
	
	Variable ret;
	int argc = (int)argv.size();
	if(argc>2) {
		throw_error(ERROR_ARGUMENTCOUNT);
	}
	if(argc==1) {
		cout<< "\t\t+------------------+\n"
			<< "\t\t|      ORATR       |\n"
			<< "\t\t+------------------+\n";
		cout << "\thttp://oratr.googlecode.com\n";
		cout << "\thttp://demosthenes2k8.endoftheinternet.org/oratr/\n";
		cout << "Implementation:\n";
		cout << "\tName: " << name << "\n";
		cout << "\tVersion: "<< version << "\n";
		cout << "Developers:\n";
		for(unsigned i=0;i<dev.size();i++) {
			cout << "\t" << dev[i].str << endl;
		}
		ret = (Variable)"";
	} else {
		string dat = argv[1].str;
		ret.type=tString;
		if(dat=="name") {
			ret = name;
		} else if(dat=="dev") {
			ret.clear();
			ret.type = tArray;
			ret.arr = dev;
		} else if(dat=="version") {
			ret = version;
		} else {
			throw_error(ERROR_ARGUMENTS);
		}
	}
	return ret;
}

Variable bi_man(vector<Variable>& argv, Environment& env)
{
	UNUSED env;
	int argc = (int)argv.size();
    Variable ret;
    string name = "";
    if(argc == 1) {
		cout << "Usage: man 'function'" << endl
		<< "\tPrints the man page for 'function'" << endl;
		return ret;
	}
    if (argv[1].type != tVarname){
		throw_error(ERROR_ARGUMENTS);
	}
	name = argv[1].str;
	if (argc > 2) {
		for (int i=2; i<argc; i++) {
			name = name + " " + argv[i].str ;	
		}
	}
	if (name == "print") {
		cout << "Usage: print [...]" << endl
		<< "\tOutputs text followed by newline" << endl;
	} else if (name == "man") {
		cout << "Usage: man 'function'" << endl
		<< "\tPrints the man page for 'function'" << endl;
	} else if (name == "echo") {
		cout << "Usage: echo [...]" << endl
		<< "\tOutputs text" << endl;
	} else if (name == "typeof") {
		cout << "Usage: typeof 'var'" << endl
		<< "\tOutputs the type of 'var'" << endl
		<< "\t\t0 = null" << endl
		<< "\t\t1 = function" << endl
		<< "\t\t2 = opcode" << endl
		<< "\t\t3 = varname" << endl
		<< "\t\t4 = number" << endl
		<< "\t\t5 = string" << endl
		<< "\t\t6 = alias" << endl
		<< "\t\t7 = inline code" << endl
		<< "\t\t8 = array" << endl
		<< "\t\t9 = unknown" << endl;
	} else if (name == "substr") {
		cout << "Usage: substr 'string' 'int1' 'int2' " << endl
		<< "\tReturns a substring of 'string' between 'int1' and 'int2'" << endl;
	} else if (name == "about") {
		cout << "Usage: about 'string'" << endl
		<< "\tstring is optional" << endl
		<< "\tOutputs Information regarding implementation of Oratr" << endl
		<< "\tWhen string is one of following, only that data is returned." << endl
		<< "\t\tname = Implementation Name" << endl
		<< "\t\tdev = Developer Name" << endl
		<< "\t\tversion = Version # of implementation" << endl;
	} else if (name == "set") {
		cout << "Usage: set 'var1' 'operation' 'var2'" << endl
		<< "\tPerforms operation on 'var1' using 'var2' as modifier" << endl
		<< "\tResult is stored in 'var1'" << endl
		<< "\tOperation can be any of the following" << endl
		<< "\t\t= Equals" << endl
		<< "\t\t+ Addition" << endl
		<< "\t\t- Subtraction" << endl
		<< "\t\t* Multiplication" << endl
		<< "\t\t/ Division" << endl
		<< "\t\t** Exponent" << endl
		<< "\t\t% Modulus" << endl
		<< "\t\t<< Bitshift left (Of integer part)" << endl
		<< "\t\t>> Bitshift right (Of integer part)" << endl
		<< "\t\t& Bitwise AND (Of integer part)" << endl
		<< "\t\t| Bitwise OR (Of integer part)" << endl
		<< "\t\t^ Bitwise XOR (Of integer part)" << endl;
	} else if (name == "math") {
		cout << "Usage: math 'var1' 'operation' 'var2' ..." << endl
		<< "\tPerforms operation on 'var1' and 'var2'" << endl
		<< "\tOperation can be any of the following" << endl
		<< "\t\t+ Addition" << endl
		<< "\t\t- Subtraction" << endl
		<< "\t\t* Multiplication" << endl
		<< "\t\t/ Division" << endl
		<< "\t\t** Exponent" << endl
		<< "\t\t% Modulus" << endl
		<< "\t\t<< Bitshift left (Of integer part)" << endl
		<< "\t\t>> Bitshift right (Of integer part)" << endl
		<< "\t\t& Bitwise AND (Of integer part)" << endl
		<< "\t\t| Bitwise OR (Of integer part)" << endl
		<< "\t\t^ Bitwise XOR (Of integer part)" << endl
		<< "\t\t== Equals" << endl
		<< "\t\t> Greater than" << endl
		<< "\t\t>= Greater than" << endl
		<< "\t\t< Less than" << endl
		<< "\t\t<= Less than" << endl
		<< "\t\t!= Not Equal" << endl
		<< "\tOperations are done in proper order, and can be more than one per line";
	} else if (name == "fread") {
		cout << "Usage: fread 'file path'" << endl
		<< "\tReturns array with every line of 'file path' in seperate index" << endl;
	} else if (name == "fecho") {
		cout << "Usage: fecho 'file path' 'var'" << endl
		<< "\tAppends 'var' to 'file path'" << endl;
	} else if (name == "get") {
		cout << "Usage: get" << endl
		<< "\tReturns value entered by user" << endl;
	} else if (name == "sleep") {
		cout << "Usage: sleep 'var'" << endl
		<< "\tSleeps interpreter for 'var' seconds" << endl;
	} else if (name == "exit") {
		cout << "Usage: exit" << endl
		<< "\tExits interpreter" << endl;
	} else if (name == "think") {
		cout << "man cannot 'think'" << endl;
	} else if (name == "if") {
		cout << "Usage: if 'var1' 'conditional' 'var2' {'inline code'} else 'else code'" << endl
		<< "\t'else code' is optional" << endl
		<< "\tRuns 'inline code' if 'var1' 'conditional' 'var2' is true" << endl
		<< "\tRuns 'else code' if 'var1' 'conditional' 'var2' is false" << endl;
	} else if (name == "alias") {
		cout << "Usage: alias 'func name' '# of arguments' {'inline code'}" << endl
		<< "\tCreates Alias 'func name' that accepts '# of arguments' arguments" << endl;
	} else if (name == "call") {
		cout << "Usage: call 'func name' 'parameters'" << endl
		<< "\tCalls alias with name 'func name' with 'parameters' arguments" << endl
		<< "\tOptional: call can be omitted" << endl;
	} else if (name == "array") {
		cout << "Usage: array 'command' 'parameters'" << endl
		<< "\t'command' can be" << endl
		<< "\t\tmake" << endl
		<< "\t\tsize" << endl
		<< "\t\tindex" << endl
		<< "\t\tset" << endl
		<< "\t\tbuild" << endl;
	} else if (name == "array make") {
		cout << "Usage: array make 'array name' 'count'" << endl
		<< "\tcreates an array with name 'name' of size 'count'" << endl;
	} else if (name == "array size") {
		cout << "Usage: array size 'array name'" << endl
		<< "\tReturns size of 'array name'" << endl;
	} else if (name == "array index") {
		cout << "Usage: array index 'array name' 'count'" << endl
		<< "\tReturns contents of 'count' position in 'array name'" << endl;
	} else if (name == "array set") {
		cout << "Usage: array set 'array name' 'count' 'operation''var'" << endl
		<< "\tPerforms 'operation' on 'count' of 'array name' and 'var'" << endl;
	} else if (name == "array build") {
		cout << "Usage: array build 'var'" << endl
		<< "\tCreates an array with the contents 'var' takes unlimited args" << endl;
	} else if (name == "env") {
		cout << "There are a number of built in variables, they all begin and end with '__'" << endl
		<< "\t\t'__return__' The value returned from last called function" << endl
		<< "\t\t'__degrees__' 1 for degrees, 0 for radians" << endl
		<< "\t\t'__tab__' the tab character" << endl
		<< "\t\t'__iterator__' the iterator value used in for loops" << endl
		<< "\t\t'__true__' represents true-usually 1" << endl
		<< "\t\t'__endl__' the newline character" << endl
		<< "\t\t'__scope__' the current scope depth" << endl
		<< "\t\t'__false__' represents false-usually 0" << endl
		<< "\t\t'__prompt__' the prompt output at begining of line" << endl
		<< "\t\t'__arrprint_start__' is printed before printing out an array" << endl
		<< "\t\t'__arrprint_end__' is printed after printing out an array" << endl
		<< "\t\t'__delimiter__' is printed between each array element" << endl;
	} else if (name == "delete") {
		cout << "Usage: delete 'type' 'name'" << endl
		<< "\tDeletes 'type' with 'name'" << endl
		<< "'type' can be one of the following" << endl
		<< "\t\tvar" << endl
		<< "\t\talias" << endl
		<< "\t\tall" << endl;
	} else if (name == "rand") {
		cout << "Usage: rand 'int1' ['int2']" << endl
		<< "\tReturns random integer between int1 and int2, if no int1 given int1 = 0" << endl;
	} else if (name == "sqrt") {
		cout << "Usage: sqrt 'var'" << endl
		<< "\tReturns the square root of 'var'" << endl;
	} else if (name == "run") {
		cout << "Usage: run 'filename'" << endl
		<< "\tRuns code in 'filename'" << endl;
	} else if (name == "import") {
		cout << "Usage: import 'filename'" << endl
		<< "\tCreates and deletes aliases and imports other files from 'filename'" << endl;
	} else if (name == "sin") {
		cout << "Usage: sin '#'" << endl
		<< "\tReturns sine '#'" << endl;
	} else if (name =="cos") {
		cout << "Usage: cos '#'" << endl
		<< "\tReturns cos '#'" << endl;
	} else if (name == "tan") {
		cout << "Usage: tan '#'" << endl
		<< "\tReturns tan '#'" << endl;
	} else if (name == "asin") {
		cout << "Usage: asin '#'" << endl
		<< "\tReturns inverse sine of '#'" << endl;
	} else if (name =="acos") {
		cout << "Usage: acos '#'" << endl
		<< "\tReturns inverse cosine of '#'" << endl;
	} else if (name == "atan") {
		cout << "Usage: atan '#'" << endl
		<< "\tReturns inverse tangent of '#'" << endl;
	} else if (name == "time") {
		cout << "Usage: time 'details'" << endl
		<< "\t'details' is a format string for data to be returned" << endl
		<< "\tValues in it are denoted by a % followed by one of the following:" << endl
		<< "\t\th = hours" << endl
		<< "\t\tm = minutes" << endl
		<< "\t\ts = seconds" << endl
		<< "\t\tD = day of month" << endl
		<< "\t\tM = month of year" << endl
		<< "\t\ty = year(last 2 digits)" << endl
		<< "\t\tY = Year(full)" << endl;
	} else if(name == "local") {
		cout << "Usage: local 'var names'" << endl
		<< "\tcreates a copy of 'var names' in the last scope" << endl;
	} else if (name == "strlen") {
		cout << "Usage: strlen 'string'" << endl
		<< "\tReturns length of 'string'" << endl;
	} else if (name == "matches") {
		cout << "Usage: matches 'a' [arr]" << endl
		<< "\tReturns true if 'a' is in 'arr'" << endl;
	} else if (name == "log") {
		cout << "Usage: log 'int1' ['int2']" << endl
		<< "\tReturns log 'int1' if 1 arg is given" << endl
		<< "\tReturns log 'int1' / log 'int2' otherwise" << endl;
	} else if (name == "path") {
		cout << "Usage: path 'command' 'parameter'" << endl
		<< "\tCommand can be any of the following" << endl
		<< "\t\tGet" << endl
		<< "\t\t\tReturns the path" << endl
		<< "\t\tSet 'filepath'" << endl
		<< "\t\t\tAdds 'filepath' to the path" << endl
		<< "\t\tDelete 'filepath'" << endl
		<< "\t\t\tDeletes 'filepath' from the path" << endl;
	} else if (name == "new") {
		cout << "Usage: new 'type' 'var'" << endl
		<< "\t'type' can be any of the following" << endl
		<< "\t\tarray" << endl
		<< "\t\t\t'var' is array size" << endl
		<< "\t\tint" << endl
		<< "\t\t\t'var' is start value" << endl
		<< "\t\tstring" << endl
		<< "\t\t\t'var' is start value" << endl
		<< "\tinline" << endl
		<< "\t\t'var' is inline code" << endl;
	} else if (name == "aget") {
		cout << "Usage: aget 'alias name'" << endl
		<< "\tReturns the contents of 'alias name'" << endl;
	} else if (name == "sprintf") {
		cout << "Usage: sprintf 'var name' 'arguments'" << endl
		<< "\tsets 'var name' equal to 'arguments' concatenated together as a string" << endl;
	} else if (name == "for") {
		cout << "Usage: for {'setup'} {'condition'} {'incr'} {'inline code'}" << endl
		<< "Usage: for 'int' {'inline code'}" << endl
		<< "\tEither syntax can be used" << endl
		<< "\tFirst style: __iterator__ starts at 'setup' incremented by 'incr'" << endl
		<< "\t\truns 'inline code' while 'condition' is true" << endl
		<< "\tSecond style: runs 'inline code' 'int' times" << endl;
	} else if (name == "while") {
		cout << "Usage: while {'conditional'} {'inline code'}" << endl
		<< "\tPerformes 'inline code' while 'conditional' is true" << endl
		<< "\t'conditional' can be one of the following:" << endl
		<< "\t\tA constant: this is an infinite loop, unless the constant is 0" << endl
		<< "\t\tInline code, which is evaluated at the start of each loop" << endl
		<< "\t\tA variable name, which is evaluated at the start of each loop" << endl;
	} else if (name == "break") {
		cout << "Usage: break" << endl
		<< "\tBreaks out of the current scope" << endl;
	} else if (name == "const") {
		cout << "Usage: const 'var' 'value'" << endl
		<< "\tLocks 'var' to 'value' so that it can't be altered" << endl;
	} else if (name == "deconst") {
		cout << "Usage: deconst 'var'" << endl
		<< "\t'var' must be a string, removes var" << endl;
	} else if (name == "error") {
		cout << "Usage: error 'int'" << endl
		<< "\tReturns error number 'int'" << endl;
	} else if (name == "global") {
		cout << "Usage: global 'var name'." << endl
		<< "\tReturns 'var name' to the global scope takes unlimited args" << endl;
	} else if (name == "timer") {
		cout << "Usage: timer 'command' 'var'" << endl
		<< "\tCommand can be:" << endl
		<< "\t\tstart" << endl
		<< "\t\tget" << endl
		<< "\t\tstop" << endl
		<< "\t\tclear" << endl;
	} else if (name == "timer start") {
		cout << "Usage: timer start 'var'" << endl
		<< "\tStarts timer with name 'var'" << endl;
	} else if (name == "timer get") {
		cout << "Usage: timer get 'var'" << endl
		<< "\tGets the value of 'var' timer in seconds" << endl;
	} else if (name == "timer stop") {
		cout << "Usage: timer stop 'var'" << endl
		<< "\tStops the timer 'var'" << endl;	
	} else if (name == "timer clear") {
		cout << "Usage: timer clear 'var'" << endl
		<< "\tStops the timer 'var'" << endl;
	} else if (name == "foreach") {
		cout << "Usage: foreach 'array' 'var' 'inline code'" << endl
		<< "\tIterates through 'array' storing the current value of 'array' in 'var'" <<endl
		<< "\t'inline code' is run each loop" << endl;
	} else if (name == "null") {
		cout << "Usage: null" << endl
		<< "\tAccepts multiple parameters, but does nothing with them" << endl
		<< "\tIf no arguments are given returns __return__" << endl
		<< "\tCan be used to sequence multiple commands inside an inline statement" << endl;
	} else if (name == "http") {
		cout << "Usage: http \"url\"" << endl
		<< "\tGets and returns the contents of a web page" << endl;
	} else if (name == "switch") {
		cout << "Usage: switch var # {code} # # {code} [default {code}]" << endl
		<< "\tIf var matches any of the #, run the {code} following it" << endl
		<< "\tAccepts any number of #" << endl;
	} else if (name == "cp") {
		cout << "Usage: cp 'src' 'dest'" << endl
		<< "\tCopies the file/folder at 'src' to 'dest'" << endl;
	} else if (name == "mv") {
		cout << "Usage: mv 'src' 'dest'" << endl
		<< "\tMoves the file/folder at 'src' to 'dest'" << endl;
	} else if (name == "rm") {
		cout << "Usage: rm 'file'" << endl
		<< "\tDeletes the file/folder at 'src'" << endl;
	} else if (name == "cwdir") {
		cout << "Usage: cwdir" << endl
		<< "\tGets the current working directory" << endl;
	} else if (name == "cd") {
		cout << "Usage: cd 'fold'" << endl
		<< "\tChanges the current working directory to 'fold'" << endl;
	} else if (name == "mkdir") {
		cout << "Usage: mkdir 'name'" << endl
		<< "\tMakes a directory in the current working directory called 'name'" << endl;
	} else if (name == "ls") {
		cout << "Usage: ls [fold]" << endl
		<< "\tPrints contents of [fold], if it exists" << endl
		<< "\tPrints contents of current working directory otherwise" << endl;
	} else {
		throw_error(ERROR_ARGUMENTS);
	}
	return ret;
}
